package com.drcurd.service.impl;

import com.drcurd.entity.Department;
import com.drcurd.mapper.DepartmentMapper;
import com.drcurd.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    
    @Autowired
    private DepartmentMapper departmentMapper;
    
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.selectAll();
    }
    
    @Override
    public Department getDepartmentById(Long id) {
        return departmentMapper.selectById(id);
    }
    
    @Override
    public boolean addDepartment(Department department) {
        return departmentMapper.insert(department) > 0;
    }
    
    @Override
    public boolean updateDepartment(Department department) {
        return departmentMapper.update(department) > 0;
    }
    
    @Override
    public boolean deleteDepartment(Long id) {
        return departmentMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<Department> getDepartmentsByPage(Integer page, Integer size) {
        int offset = (page - 1) * size;
        return departmentMapper.selectByPage(offset, size);
    }
    
    @Override
    public int getDepartmentCount() {
        return departmentMapper.selectCount();
    }
    
    @Override
    public com.github.pagehelper.PageInfo<Department> getDepartmentListByCondition(com.drcurd.dto.DepartmentQueryDTO queryDTO) {
        // 计算偏移量
        int offset = (queryDTO.getPageNum() - 1) * queryDTO.getPageSize();
        
        // 设置偏移量和大小
        queryDTO.setOffset(offset);
        queryDTO.setSize(queryDTO.getPageSize());
        
        // 查询数据
        List<Department> list = departmentMapper.selectByCondition(queryDTO);
        
        // 查询总数
        int total = departmentMapper.selectCountByCondition(queryDTO);
        
        // 创建PageInfo对象
        com.github.pagehelper.PageInfo<Department> pageInfo = new com.github.pagehelper.PageInfo<>(list);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(queryDTO.getPageNum());
        pageInfo.setPageSize(queryDTO.getPageSize());
        
        return pageInfo;
    }
} 